import java.util.*;
import java.io.*;
class Cache implements Comparable<Cache> {
	private String realmname;//����
	private String ip;//ip
	private String timestamp;//ʱ���
	private int count;//��������

	
	public Cache(String realmname, String ip, String timestamp, int count) {
		this.realmname = realmname;
		this.ip = ip;
		this.timestamp = timestamp;
		this.count = count;
	}

	public String getRealmname() {
		return realmname;
	}

	public void setRealmname(String realmname) {
		this.realmname = realmname;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
	@Override
	public String toString() {
		return ("Cache [realmname=" + realmname + ", ip=" + ip+ ", timestamp=" + timestamp+", count=" + count+"]");
	}

	@Override
	public int compareTo(Cache cache) {          
		return this.count - cache.getCount();
	}
}

class MyRunnable implements Runnable{
	public Long border=new Long(20000);//ˢ�»����ʱ����20s�������Լ�����
	@Override
    public void run() {
		try {
			while(true) {
				synchronized(Point.synObj)
				{
				Date date= new Date();
				Long currenttime= Long.valueOf(String.valueOf(date.getTime()));
				Long min=new Long(0);
				if (!Point.buffertable.isEmpty())
					min=Long.valueOf(Point.buffertable.get(0).getTimestamp());
				for(int i=0;i<Point.buffertable.size();i++) {
					if(currenttime-Long.valueOf(Point.buffertable.get(i).getTimestamp())>=border) {
						Point.buffertable.remove(i);
						i--;
					}
					else if(min>Long.valueOf(Point.buffertable.get(i).getTimestamp()))
						min=Long.valueOf(Point.buffertable.get(i).getTimestamp());
				}
				Thread.currentThread().sleep(border-currenttime+min);
				}
			}
			
			} catch (Exception e) {
				e.printStackTrace();
			}
	}
}
public class Point {
	public static List<Cache> buffertable = new ArrayList<Cache>();//�����
	public static int length=100;//��󻺴�����100���������Լ�����
	public static Object synObj = new Object();//ͬ����
	public static void main(String[] args) {
		
		String realmname;//����
		String ip;//ip
		String timestamp;//ʱ���
		int count;//��������
		Cache buffer;//�������


		Date date;
		String time;	
		date= new Date();//��ȡ��ǰʱ��
		timestamp= String.valueOf(date.getTime());  
		//������ֻ��Ҫ�Ѹ��յ���������ip�������µ�realmname��ip�����಻�ø�
		realmname="baidu";//�յ�������
		ip="6.2.3.0";//�յ���ip
		count=1;//����ֵ�����ø�
		synchronized(synObj) 
		{
			if (buffertable.size() < length) {//�����û�����¼�¼ֱ�Ӽ���
				buffer=new Cache(realmname,ip,timestamp,count);
				buffertable.add(buffer);
			}
			else
			{
				int i;
				for(i=0;i<buffertable.size();i++) {
					if(buffertable.get(i).getRealmname().equals(realmname))
						break;
				}
				if(i<buffertable.size()) {//���л����¼������������һ
					buffertable.get(i).setCount(buffertable.get(i).getCount()+1);
					buffertable.get(i).setTimestamp(timestamp);
				}
				else {//�滻��С���������Ļ����¼
					Collections.sort(buffertable);
					buffertable.remove(buffertable.size()-1);
					buffer=new Cache(realmname,ip,timestamp,count);
					buffertable.add(buffer);		
					System.out.println(buffertable.size());
				}
			}
		}
		Thread mythread=new Thread(new MyRunnable());//�̣߳����ݻ����Ƿ�洢����20s�������Ƿ�ɾ�������¼
		mythread.start();
	}

}
 